package com.liuhongwei.test.builder;

import com.liuhongwei.test.bean.Constants;
import com.liuhongwei.test.bean.TableInfo;
import com.liuhongwei.test.builder.strategy.enums.MapperXmlEnum;
import com.liuhongwei.test.builder.strategy.factory.MapperXmlFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;


public class BuildExtendMapperXml {

    private static final Logger log = LoggerFactory.getLogger(BuildExtendMapperXml.class);
    public static final String BASE_RESULT_MAP = "PageResultMap";
    public static final String BASE_COLUMN_LIST = "Page_Column_List";
    public static final String BASE_QUERY_CONDITION = "Page_condition";
    public static final String BASE_QUERY_CONDITION_EXTEND = "Page_condition_extend";
    public static final String QUERY_CONDITION = "Page_Where_Condition";

    public static final String SELECT_PREFIX = "query.";
    public static final String INSERT_PREFIX = "bean.";


    public static void execute(TableInfo tableInfo) {
        File folder = new File(Constants.PATH_MAPPERS_XML);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            String beanName = tableInfo.getBeanName() + Constants.SUFFIX_MAPPER_EXTEND;

            os = new FileOutputStream(folder + "/" + beanName + ".xml");
            osw = new OutputStreamWriter(os, "UTF-8");
            bw = new BufferedWriter(osw);

            //写入属性
            writeField(tableInfo, bw, beanName);

            bw.flush();
        } catch (Exception e) {
            log.error("创建mapper_xml失败", e);
        } finally {
            BuildBase.close(os, osw, bw);
        }
    }

    private static void writeField(TableInfo tableInfo, BufferedWriter bw, String beanName) throws Exception {
        bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        bw.write("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"\n");
        bw.write("\t\"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n");
        bw.write("<mapper namespace=\"" + Constants.PACKAGE_MAPPERS + "." + beanName + "\">\n");

        for (MapperXmlEnum value : MapperXmlEnum.values()) {
            MapperXmlFactory.buildXml(value, tableInfo, bw);
        }

        bw.write("</mapper>");
    }

    public static String getPo(TableInfo tableInfo) {
        return Constants.PACKAGE_PO + "." + tableInfo.getBeanName();
    }

    public static String includeBaseColumnList() {
        return "<include refid=\"" + BASE_COLUMN_LIST + "\"/>";
    }

    public static String includeQueryCondition() {
        return "<include refid=\"" + QUERY_CONDITION + "\"/>";
    }

    public static void main(String[] args) {
        List<TableInfo> tableInfoList = BuildTable.getTable();
        for (TableInfo tableInfo : tableInfoList) {
            BuildExtendMapperXml.execute(tableInfo);
        }
    }

}
